package com.sfang.common.util.string;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.TreeMap;

public class SginatureUtil {

    public static String appSecret="4e0a48e1c0b54041bce9c8f0e036124d";

    public static String getSign(HttpServletRequest request, Map<String, String> bodys,String appSecret){

        String bodystr = request.getMethod()+canonicalize(bodys);
        String signing = appSecret+bodystr+appSecret;
        return MD5Util.EncryptionStr(signing);
    }

    public static Boolean postSign(HttpServletRequest request) throws IOException {
        String requestURL = request.getRequestURL().toString();
        String gsn="";

        String[] param = requestURL.split("\\?")[1].split("&");
        Map<String,String> bodys =new TreeMap<>();
        for(String s:param){
            String[] ms = s.split("=");
            if ("gsn".equals(ms[0])){
                gsn=ms[1];
                continue;
            }
            bodys.put(ms[0], ms[1]);
        }

        BufferedReader br = request.getReader();
        String str, wholeStr = "";
        while((str = br.readLine()) != null){
            wholeStr += str;
        }
        bodys.put("body",wholeStr);

        String bodystr = request.getMethod()+canonicalize(bodys);
        String signing = appSecret+bodystr+appSecret;

        if (gsn.equals(MD5Util.EncryptionStr(signing)))
            return true;

        return false;
    }

    public static String canonicalize(Map<String, String> bodys){
        String bodystr= "";

        for (Map.Entry<String, String> entry : bodys.entrySet()) {
            String key=entry.getKey();
            String value=entry.getValue();
            System.out.println(entry.getKey()+","+entry.getValue());
            if ("body".equals(entry.getKey())){
                value=URLEncoder.encode(entry.getValue()).replace("+","");
            }
            bodystr=bodystr+key+value;
        }

        return bodystr;
    }



    public static void main(String[] args) {
        String appSecret="4e0a48e1c0b54041bce9c8f0e036124d";

        Map<String,String> map =new TreeMap<>();
//        map.put("mangaId","33991");
        map.put("gsm","md5");
        map.put("yq_is_anonymous","0");
        map.put("gak","ios_manhuaren2");
        map.put("gft","json");
        map.put("gts","2018-11-16%2008%3A54%3A15");
        map.put("gut","1");
        map.put("gui","1286438");
        map.put("gat","");
        map.put("gaui","1285918");
        map.put("body","{\n  \"sex\" : \"m\",\n  \"nickname\" : \"xxx163500\"\n \"}");

        String bodystr= "";
        for (Map.Entry<String, String> entry : map.entrySet()) {
            String key=entry.getKey();
            String value=entry.getValue();
            if ("body".equals(entry.getKey())){
                value=URLEncoder.encode(entry.getValue());
            }
            bodystr=bodystr+key+value;
        }

//        bodystr=URLEncoder.encode("{\n \b \"sex\" : \"m\",\n  \"nickname\" : \"xxx163500\"\n \"}");
//        System.out.println(bodystr.replace("+",""));

        String signing = appSecret+"POST"+bodystr+appSecret;
        System.out.println(signing);

        System.out.println("signing:"+MD5Util.EncryptionStr(signing));
    }

}
